package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.mapper.ChatHistoryMapper;
import com.chixing.pojo.ChatHistory;
import com.chixing.service.IChatHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xyh
 * @since 2022-11-02
 */
@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements IChatHistoryService {

    @Autowired
    private ChatHistoryMapper chatHistoryMapper;

    @Override
    public String getChatContent(ChatHistory chatHistory) {
        QueryWrapper<ChatHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("sponsor_name", chatHistory.getSponsorName()).
                eq("recipient_name", chatHistory.getRecipientName());
        ChatHistory chatHistory1 = chatHistoryMapper.selectOne(wrapper);
        if (chatHistory1 != null) {
            String chatText = chatHistory1.getChatContent();
            return chatText;
        }
        return null;
    }

    @Override
    public boolean updateChatContent(ChatHistory chatHistory) {
        QueryWrapper<ChatHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("sponsor_name", chatHistory.getSponsorName()).
                eq("recipient_name", chatHistory.getRecipientName());
        ChatHistory chatHistory1 = chatHistoryMapper.selectOne(wrapper);
        if (chatHistory1 != null) {
            chatHistory1.setChatContent(chatHistory.getChatContent());
            chatHistory1.setChatStatus(chatHistory.getChatStatus());
            int i = chatHistoryMapper.updateById(chatHistory1);
            return i > 0;
        }else {
            int insert = chatHistoryMapper.insert(chatHistory);
            return insert>0;
        }
    }

    //获得聊天状态是否已读
    @Override
    public Boolean getChatStatus(ChatHistory chatHistory) {
        QueryWrapper<ChatHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("sponsor_name", chatHistory.getSponsorName()).
                eq("recipient_name", chatHistory.getRecipientName());
        ChatHistory chatHistory1 = chatHistoryMapper.selectOne(wrapper);
        if(chatHistory1!=null) {
            if (chatHistory1.getChatStatus() == 0)
                return false;
        }else {
            return false;
        }
        return true;
    }

    //更新小红点，返回未读消息的数量
    @Override
    public Integer getChatStatusNumber(String username) {
        QueryWrapper<ChatHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("sponsor_name",username);
        List<ChatHistory> chatHistories = chatHistoryMapper.selectList(wrapper);
        Integer unreadMessage = 0;
        if(chatHistories.size() != 0) {
            for (ChatHistory chatHistory : chatHistories) {
                if (chatHistory.getChatStatus() == 0) {
                    unreadMessage += 1;
                }
            }
        }
        return unreadMessage;
    }
}
